export function compareTimestamp(timestamp) {
	const currentTime = new Date().getTime();
	const timeDiff = currentTime - timestamp;

	if (timeDiff < 60000) {
		return '1分钟内';
	} else if (timeDiff < 3600000) {
		return Math.floor(timeDiff / 60000) + '分钟';
	} else if (timeDiff < 86400000) {
		return Math.floor(timeDiff / 3600000) + '小时';
	} else if (timeDiff < 2592000000) {
		return Math.floor(timeDiff / 86400000) + '天';
	} else if (timeDiff < 7776000000) {
		return Math.floor(timeDiff / 2592000000) + '月';
	} else {
		return null;
	}
}


//提示框
export function showToast({
	title = "",
	duration = 1500,
	icon = "none",
	mask = false
} = {}) {
	uni.showToast({
		title: String(title),
		icon,
		duration,
		mask
	})
}

export function gotoHome() {
	uni.showModal({
		title: "提示",
		content: "页面有误将返回首页",
		showCancel: false,
		success: (res) => {
			if (res.confirm) {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			}
		}
	})
}


const startScore = 1000;

export function getUserScore() {
	try {
		const value = uni.getStorageSync('myScore');
		console.log('myScore:' + value);
		if (value && value >= 0) {
			console.log('myScore1:' + value);
			return value;
		} else {
			console.log('myScore2:' + value);
			uni.setStorageSync('myScore', ''+startScore);
			return startScore;
		}
	} catch (e) {
		// error
		return startScore;
	}
}

export function addUserScore(score) {
	try {
		let value = uni.getStorageSync('myScore');
		let sum = Number(value) + score;
		console.log('sum:'+sum);
		if (sum < 0) {
			console.log('sum1:'+sum);
			// uni.setStorageSync('myScore', 0);
			uni.showToast({
				title: '积分不够了',
				icon: "none",
				duration: 1500
			})
			return -1;
		}
		else {
			console.log('sum2:'+sum);
			uni.setStorageSync('myScore', ''+sum);
			return sum;
		}
	} catch (e) {
		// error
		uni.showToast({
			title: '数据异常，请稍后再试',
			icon: "none",
			duration: 1500
		})
		return -1;
	}
}


export function SignInGetScore() {
	let today = new Date().getFullYear() + new Date().getMonth() + new Date().getDay()
	try {
		const value = uni.getStorageSync('SignIn');
		if (value === today) {
			uni.showToast({
				title: '已签到，明天再来试试吧',
				icon: "none",
				duration: 1500
			})
			return getUserScore();
		} else {
			uni.showToast({
				title: '签到成功',
				icon: "success",
				duration: 1500
			})
			uni.setStorageSync('SignIn', today)
			const value = uni.getStorageSync('myScore');
			if (value && value > 0) {
				console.log('myScore:' + value);
				uni.setStorageSync('myScore', ''+ value + startScore)
			} else {
				uni.setStorageSync('myScore', ''+ startScore);
			}
		}
		return getUserScore();
	} catch (e) {
		// error
		console.log('myScore' + e);
		return getUserScore();
	}
}